import Vue from 'vue'
import Router from 'vue-router'
import Menu from '../views/Menu'
import Project from '../views/Project'
import TestCase from '../views/testCase/TestCaseIndex'
import APi from '../views/apiPage/Api'
import test from '../components/test'
import VueRouter from 'vue-router'
import StepPage from '../views/testCase/StepPage'
import GroupPage from '../views/groupPage/index.vue'
import HexSend from '../views/HeXIndex'
import task from '../views/task/index.vue'
import report from '../views/report/index.vue'
import login from '../views/login/index.vue'
import jobDetail from "../views/task/jobDetailPage.vue"
import reportDetail from '../views/report/reportDetail.vue'
import env from '../views/EvnPage/index.vue'
import envParams from '../views/EnvParamsPage/index.vue'
import dbConnection from '../views/DBConnectionPage/index.vue'
import mqttTest from '../views/TestPage/MqttTestPage'
import UITestCase from '../views/uiTestCasePage/UITestCaseIndex'
import store from "../store";
import el from "element-ui/src/locale/lang/el";

Vue.use(Router)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router= new Router({
  routes: [
    {
      path: '/Menu',
      name: 'Menu',
      meta: { title: '首页',keepAlive: true },
      component: Menu,
      children: [
        {
          path: '/Project',
          name: 'Project',
          meta: { title: '项目管理' ,keepAlive: true },
          component: Project,
        },
        {
          path: '/APi',
          name: 'APi',
          meta: { title: '接口管理', keepAlive: true },
          component: APi,
        },
        {
          path: '/TestCase',
          name: 'TestCase',
          meta: { title: '用例管理' ,keepAlive: true},
          component: TestCase,
        },
        {
          path: '/UITestCase',
          name: 'UITestCase',
          meta: { title: 'UI用例管理' ,keepAlive: true},
          component: UITestCase,
        },
        {
          path: '/StepPage',
          name: 'StepPage',
          meta: { title: '编辑用例',keepAlive: true },
          component: StepPage,

        },
        {
          path: '/GroupPage',
          name: 'GroupPage',
          meta: { title: '用例集管理',keepAlive: true },
          component: GroupPage,
        },
        {
          path: '/HexSend',
          name: 'HexSend',
          meta: { title: 'hex发送测试',keepAlive: true },
          component: HexSend,

        },
        {
          path: '/task',
          name: 'task',
          meta: { title: '执行任务管理',keepAlive: true },
          component: task,

        },
        {
          path: '/report',
          name: 'report',
          meta: { title: '报表管理' ,keepAlive: true},
          component: report,

        },
        {
          path: '/reportDetail',
          name: 'reportDetail',
          meta: {title: '报表详情' ,keepAlive: true},
          component: reportDetail,

        },
        {
          path: '/jobDetail',
          name: 'jobDetail',
          meta: { title: '任务详情' ,keepAlive: true},
          component: jobDetail,

        },
        {
          path: '/env',
          name: 'env',
          meta: { title: '环境管理' ,keepAlive: true},
          component: env,

        },
        {
          path: '/dbConnection',
          name: 'dbConnection',
          meta: { title: '数据库连接管理' ,keepAlive: true},
          component: dbConnection,

        },
        {
          path: '/envParams',
          name: 'envParams',
          meta: { title: '环境参数管理' ,keepAlive: true},
          component: envParams,

        },
        {
          path: '/mqttTest',
          name: 'mqttTest',
          meta: { title: 'mqtt测试' ,keepAlive: true},
          component: mqttTest,

        }

      ]
    },
    {path: '/login',
      name: 'Login',
      meta: { title: '登录' ,keepAlive: true},
      component: login,}
  ],


},);

function requireAuth(to, from, next) {
  if (to.meta.requireAuth) {
    if (store.state.token) {
      console.log("test")
      next();
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      });
    }
  } else {
    next();
  }
}

router.beforeEach(requireAuth);
// router.beforeEach((to,from,next) =>{
//   if (to.meta.requireAuth){
//     if (store.state.token){
//       console.log("test")
//       next();
//     }else {
//       next({
//         path: '/login',
//         query:{redirect : to.fullPath}
//       });
//     }
//   }else{
//     next();
//   }
// })

export default router;

